package com.mobjam.workingtitleforagame;

import java.util.Stack;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;

public class MapTile {
	private static Bitmap grassTile = null;
	private static Bitmap waterTile = null;
	private static Bitmap sandTile =  null;
	private static Bitmap rockTile = null;

	private MapPortal mapPortal = null;
	
	public static void loadBitmaps( Context context )
	{
		if( grassTile == null )
		{
			grassTile = BitmapFactory.decodeResource( context.getResources(), R.drawable.grass);
		}
		
		if( waterTile == null )
		{
			waterTile = BitmapFactory.decodeResource(context.getResources(), R.drawable.water);
		}
		
		if( sandTile == null )
		{
			sandTile = BitmapFactory.decodeResource(context.getResources(), R.drawable.sand);
		}

		if( rockTile == null )
		{
			rockTile = BitmapFactory.decodeResource(context.getResources(), R.drawable.rock);
		}
	}
	
	public int getInRegionX() {
		return inRegionX;
	}

	public void setInRegionX(int inRegionX) {
		this.inRegionX = inRegionX;
	}

	public int getInRegionY() {
		return inRegionY;
	}

	public void setInRegionY(int inRegionY) {
		this.inRegionY = inRegionY;
	}

	public void launchPortal()
	{
		if( mapPortal != null )
		{
			mapPortal.launchActivity();
		}
	}
	
	public boolean containsPortal( )
	{
		for( MapObject mapObject : this.childMapObjects )
		{
			if( mapObject instanceof MapPortal )
			{
				return true;
			}
		}
		
		return false;
	}

	public boolean moveObjectTo(MapObject toMove, MapTile destination) 
	{
		if( destination == null )
		{
			return false;
		}
		
		if( !childMapObjects.contains( toMove ))
		{
			return false;
		}
		
		if( !destination.addChildMapObject(toMove))
		{
			return false;
		}
		
		this.removeChildMapObject(toMove);
		toMove.setCurrentTile(destination);
		if( destination.containsPortal() )
		{
			destination.launchPortal();
		}
		return true;
	}

	public boolean moveObjectNorth(MapObject toMove)
	{
		if( northTile == null )
			return false;
		
		if( !childMapObjects.contains(toMove))
		{
			return false;
		}
		
		if( !northTile.addChildMapObject(toMove) )
		{
			return false;
		}
		this.removeChildMapObject(toMove);
		toMove.setCurrentTile(northTile);
		if( northTile.containsPortal())
		{
			northTile.launchPortal();
		}
		return true;
	}
	
	public boolean moveObjectSouth(MapObject toMove)
	{
		if( southTile == null )
			return false;
		
		if( !childMapObjects.contains(toMove))
		{
			return false;
		}
		Log.d("MapTile", "move object south");
		if( !southTile.addChildMapObject(toMove) )
		{
			return false;
		}
			this.removeChildMapObject(toMove);
		toMove.setCurrentTile(southTile);
		if( southTile.containsPortal())
		{
			southTile.launchPortal();
		}
		return true;
	}

	public boolean moveObjectEast(MapObject toMove)
	{
		if( eastTile == null )
			return false;
		
		if( !childMapObjects.contains(toMove))
		{
			return false;
		}
		if( !eastTile.addChildMapObject(toMove) )
		{
			return false;
		}
		
		this.removeChildMapObject(toMove);
		toMove.setCurrentTile(eastTile);
		if( eastTile.containsPortal())
		{
			eastTile.launchPortal();
		}
		return true;
	}

	public boolean moveObjectWest(MapObject toMove)
	{
		if( westTile == null )
			return false;
		
		if( !childMapObjects.contains(toMove))
		{
			return false;
		}
		
		if( !westTile.addChildMapObject(toMove) )
		{
			return false;
		}
		this.removeChildMapObject(toMove);
		toMove.setCurrentTile(westTile);
		if( westTile.containsPortal())
		{
			westTile.launchPortal();
		}
		return true;
	}
	
	public MapTile getNorthTile() {
		return northTile;
	}

	public void setNorthTile(MapTile northTile) {
		this.northTile = northTile;
	}

	public MapTile getSouthTile() {
		return southTile;
	}

	public void setSouthTile(MapTile southTile) {
		this.southTile = southTile;
	}

	public MapTile getEastTile() {
		return eastTile;
	}

	public void setEastTile(MapTile eastTile) {
		this.eastTile = eastTile;
	}

	public MapTile getWestTile() {
		return westTile;
	}

	public void setWestTile(MapTile westTile) {
		this.westTile = westTile;
	}

	private Stack<MapObject> childMapObjects;
	private int tileColor;
	private Bitmap tileBitmap;
	private int tileType;
	private MapTile northTile;
	private MapTile southTile;
	private MapTile eastTile;
	private MapTile westTile;
	private int inRegionX;
	private int inRegionY;
	private boolean isPassable;
	
	public MapTile( Context parentContext, int tileWidth, int tileHeight, int inRegionX, int inRegionY )
	{
		this.childMapObjects = new Stack<MapObject>();
		
		this.inRegionX = inRegionX;
		this.inRegionY = inRegionY;
		
		int color = (int)(Math.random() * 1000);

		if( color < 700 )
		{
			tileType = 0;
			tileBitmap = grassTile;
			isPassable = true;
				tileColor = Color.GREEN;
		}
		else if( color < 750 )
		{
			tileBitmap = waterTile;
			tileType = 1;
			isPassable = false;
				tileColor = Color.BLUE;
		}
		else if( color < 875 )
		{
			tileBitmap = rockTile;
			tileType = 2;
			isPassable = false;
				tileColor = Color.DKGRAY;
		}
		else
		{
			tileBitmap = sandTile;
			isPassable = true;
			tileType = 3;
				tileColor = Color.LTGRAY;
		}
	}

	/**
	 * @return the childMapObject
	 */
	public Stack<MapObject> getChildMapObjects() {
		return childMapObjects;
	}

	/**
	 * @param childMapObject the childMapObject to set
	 */
	public void setChildMapObjects(Stack<MapObject> childMapObjects) {
		this.childMapObjects = childMapObjects;
	}

	/**
	 * @return the tileColor
	 */
	public int getTileColor() {
		return tileColor;
	}

	/**
	 * @param tileColor the tileColor to set
	 */
	public void setTileColor(int tileColor) {
		this.tileColor = tileColor;
	}

	public boolean addChildMapObject( MapObject toAdd )
	{
		if( this.childMapObjects == null )
		{
			childMapObjects = new Stack<MapObject>();
		}
				
		if( isPassable )
		{
			childMapObjects.add( toAdd);
			return true;
		}
		
		return false;
	}
	
	public String toString( )
	{
		String childObjects = "";
		if( !childMapObjects.empty() )
		{
			childObjects = " with ";
		}
		
		for( MapObject mapObject : this.childMapObjects )
		{
			childObjects.concat( mapObject.toString());
		}
		
		return String.format( "%1 tile%2", String.valueOf(tileColor), childObjects );
	}

	public void removeChildMapObject(MapObject mapObject) 
	{
		childMapObjects.remove(mapObject);		
	}

	/**
	 * @return the tileType
	 */
	public int getTileType() {
		return tileType;
	}
	
	public Bitmap getTileBitmap()
	{
		return tileBitmap;
	}

	/**
	 * @param tileType the tileType to set
	 */
	public void setTileType(int tileType) {
		this.tileType = tileType;
	}

	public boolean getIsPassable() {
		return isPassable;
	}

	public void setIsPassable(boolean isPassable) {
		this.isPassable = isPassable;
	}

	public void addPortal(MapPortal mapPortal) 
	{
		this.mapPortal = mapPortal;
		this.addChildMapObject(mapPortal);
	}
}
